package com.xbongbong.paas.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ConditionGetDTO;
import com.xbongbong.paas.pojo.dto.ConditionListDTO;
import com.xbongbong.paas.pojo.vo.ConditionGetVO;
import com.xbongbong.paas.pojo.vo.ConditionListVO;
import com.xbongbong.paas.service.ConditionService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @desrcption:
 * @author long.rao
 * @date 2020-07-29 14:08
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/mobile/condition")
public class MobileConditionController {

    @Resource
    private ConditionService conditionService;

    /**
     * 根据字段类型检索条件
     * @param conditionGetDTO
     * @param br
     * @return
     */

    @RequestMapping(value = "/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String get(@RequestBody @Valid ConditionGetDTO conditionGetDTO, BindingResult br){
        XbbResponse<ConditionGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ConditionGetVO conditionGetVO = conditionService.get(conditionGetDTO);
                response = new XbbResponse<>(conditionGetVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * 获取流转条件
     * @param conditionListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(@RequestBody @Valid ConditionListDTO conditionListDTO, BindingResult br) {
        XbbResponse<ConditionListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ConditionListVO conditionListVO = conditionService.list(conditionListDTO);
                response = new XbbResponse<>(conditionListVO);
            } catch (XbbException e){
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

}
